package com.boge.parse.web;

import java.nio.charset.Charset;

import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;

// https://sg.jianshu.io/p/41f24a868c36 解析socket协议参考网址

/**
 * websocket 过滤器
 * @author boge
 */
public class WebSocketCodecFilter implements ProtocolCodecFactory {

	private ProtocolEncoder encoder;
	private ProtocolDecoder decoder;

	public WebSocketCodecFilter() {
		this(Charset.defaultCharset());
	}
	
	/**
	 * 数据解析
	 * @param isServer 是否是服务器
	 */
	public WebSocketCodecFilter(boolean isServer) {
		this(Charset.defaultCharset(), isServer);
	}

	/**
	 * 数据解析
	 * @param defaultCharset 默认语言
	 */
	public WebSocketCodecFilter(Charset defaultCharset) {
		this(defaultCharset, true);
	}

	/**
	 * 数据解析
	 * @param defaultCharset 默认语言
	 * @param isServer 是否是服务器
	 */
	public WebSocketCodecFilter(Charset defaultCharset, boolean isServer) {
		decoder = new WebSocketDecoder(defaultCharset, isServer);
		encoder = new WebSocketEncoder(isServer);
	}

	@Override
	public ProtocolEncoder getEncoder(IoSession ioSession) throws Exception {
		return encoder;
	}

	@Override
	public ProtocolDecoder getDecoder(IoSession ioSession) throws Exception {
		return decoder;
	}
}
